Hive日期时间函数 您所在的位置:网站首页 hive date函数取前30天 Hive日期时间函数

Hive日期时间函数

2024-05-26 20:51| 来源: 网络整理| 查看: 265

1.取得当前日期:

select current_date(); --返回类型'yyyy-mm-dd',如今天日期'2020-01-01'

2.取得当前日期时间:

select current_timestamp(); --返回格式'yyyy-mm-dd hh:mi:ss' 如'2021-07-26 15:01:31'

3.hive取得当前时间戳:

select unix_timestamp(); --返回格式为'1627282950'

4.时间戳转日期:

select from_unixtime(1517725479,'yyyy-MM-dd HH:dd:ss');

5.日期转unix时间戳:

select to_nuix_timestamp('2017-01-01 12:12:12','yyyy-MM-dd HH:dd:ss');

7.hive取得当前时间:

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');

 2、hive自动计算其他日期(昨天,今天):

hive中日期加减函数:date_add(start_date,num_days)

1. 取得昨天日期:

select date_add(current_date(),-1)  -- 返回'2020-07-13' select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1); --返回'2020-07-13' select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1); select date_format(date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);

2.  取得明天日期:

select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1); select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);

3. hive取得两个日期之间差值(差值为天数):

datediff(date1,date2):date1大于date2,返回值为正,否则,返回值为负。 select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-10)); select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),10));

4. 字符串转时间(字符串必须为:yyyy-MM-dd格式)

select to_date('2017-01-01 12:12:12');

5. 日期、时间戳、字符串类型格式化输出标准时间格式:

select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss'); select date_format(current_date(),'yyyyMMdd'); select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss'); --字符串必须满足yyyy-MM-dd格式

6. utc时间转换:

select from_utc_timestamp(current_timestamp(),8); select to_utc_timestamp(current_timestamp(),8);

 二,常用日期处理-昨天、本月、上月同期、去年同期、月初、月末

 以当前系统日期作为参考日期,将其转换为统一的天维度yyyyMMdd数据格式。例如当前日期为2021-01-29

本月月初 select from_unixtime(unix_timestamp(), 'yyyyMM') 202101 上月同期 select date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-1),'yyyyMMdd') 20201229 上月月初 select date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-1),'yyyyMMdd') 20201229 上月月初 select CONCAT(date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-1),'yyyyMM'),'01') 20201201 上月月末 select date_format(date_sub(from_unixtime(unix_timestamp(CONCAT(from_unixtime(unix_timestamp(), 'yyyyMM'),'01'), 'yyyyMMdd'), 'yyyy-MM-dd'),1),'yyyyMMdd') 20201231 去年同期 select date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-12),'yyyyMMdd') 20200129 去年同期月初 select CONCAT(date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-12),'yyyyMM'),'01') 20200101 二、参考日期为指定日期

指定时间格式为yyyMMdd

昨天 select date_format(date_sub(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),1),'yyyyMMdd') 20210128 本月月初 select CONCAT(substr('20210129',1,6),'01') 20210101 上月同期 select date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-1),'yyyyMMdd') 20201229 上月月初 select CONCAT(date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-1),'yyyyMM'),'01') 20201201 上月月末 select date_format(date_sub(from_unixtime(unix_timestamp(CONCAT(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyyMM'),'01'), 'yyyyMMdd'), 'yyyy-MM-dd'),1),'yyyyMMdd') 20201231 去年同期 select date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-12),'yyyyMMdd') 20200129 去年同期月初 select CONCAT(date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-12),'yyyyMM'),'01') 20200101

 

 参考:Hive 常用日期处理-昨天、本月、上月同期、去年同期、月初、月末等_Hive_大数据知识库 (saoniuhuo.com)

 

 

 

 

 

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有